<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 活动页类
 * @package  module
 * @author   create by allen.qiang
 * @version 1.0
 */

class contentCenter {

    /**
     * 查询内容中心信息
     * @param array $arg_array                          查询参数数组
     * @return null
     */
    public function searchContentCenterList( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //内容中心ID
        if (isset($arg_array['contentID']) && is_numeric($arg_array['contentID'])) {
            $sql .= ' AND contentID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['contentID'];
        }

        //内容中心标题
        if (isset($arg_array['contentTitle']) && !empty($arg_array['contentTitle'])) {
            $sql .= ' AND contentTitle LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['contentTitle']) . '%';
        }

        //使用状态
        if (isset($arg_array['useFlg']) && is_numeric($arg_array['useFlg'])) {
            $sql .= ' AND useFlg = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['useFlg'];
        }

        //排序号
        if (isset($arg_array['contentSort']) && !empty($arg_array['contentSort'])) {
            $sql .= ' AND contentSort LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['contentSort']) . '%';
        }

        //内容中心
        if (isset($arg_array['contentSummary']) && !empty($arg_array['contentSummary'])) {
            $sql .= ' AND contentSummary LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['contentSummary']) . '%';
        }

        //关键字
        if (isset($arg_array['searchKeys']) && !empty($arg_array['searchKeys'])) {
            $sql .= ' AND searchKeys = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['searchKeys']);
        }

        //头部
        if (isset($arg_array['isethead']) && !empty($arg_array['isethead'])) {
            $sql .= ' AND isethead LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['isethead']) . '%';
        }

        //尾部
        if (isset($arg_array['isetstern']) && !empty($arg_array['isetstern'])) {
            $sql .= ' AND isetstern LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['isetstern']) . '%';
        }

        //分页
        if (isset($arg_array['isetpage']) && !empty($arg_array['isetpage'])) {
            $sql .= ' AND isetpage LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['isetpage']) . '%';
        }

        //关键词
        if (isset($arg_array['keyword']) && !empty($arg_array['keyword'])) {
            $sql .= ' AND keyword LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['keyword']) . '%';
        }

        //商品网购好串
        if (isset($arg_array['postIDStr']) && !empty($arg_array['postIDStr'])) {
            $sql .= ' AND postIDStr LIKE ?';
            $params_type .= 's';
            $params_array[] = '%' . trim($arg_array['postIDStr']) . '%';
        }

        //add by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---start
        //城市ID
        if (isset($arg_array['cityID']) && is_numeric($arg_array['cityID'])) {
            $sql .= ' AND cityID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['cityID'];
        }
        //add by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---end

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计分类数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM content_center ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                $arg_array['count'] = 0;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM content_center ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array) {
                return null;
            }

            $item_array = array();

            foreach($temp_item_array as &$item) {
                $item_array[] = array(
                    'createTime' => $item->createTime,
                    'contentID' => $item->contentID,
                    'contentTitle' => $item->contentTitle,
                    'content_pic' => $item->content_pic,
                    'content_pic_show' => $item->content_pic ? G_IMAGE_URL . $item->content_pic : '',
                    'useFlg' => $item->useFlg,
                    'contentSort' => $item->contentSort,
                    'contentSummary' => $item->contentSummary,
                    'searchKeys' => $item->searchKeys,
                    'description' => $item->description,
                    'isethead' => $item->isethead,
                    'isetstern' =>$item->isetstern,
                    'url_link' => $item->url_link,
                    //add by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---start
                    'cityID' =>$item->cityID,
                    'cityName' => $item->cityName,
                    //add by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---end
                    'page_link' => $item->url_link ? $item->url_link : G_WEBSITE_DOMAINNAME . sprintf('/shop/contentCenter-%d.html', $item->contentID)
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 查询内容中心详情
     * @param $contentID
     * @return null
     */
    public function searchContentCenterDetail( $contentID ) {

        if (!isset($contentID) || !quanli_is_int($contentID)) {
            return null;
        }

        $args_array = array(
            'contentID' => $contentID
        );
        $content_center_array = $this->searchContentCenterList( $args_array );
        if ($content_center_array) {
            return $content_center_array[0];
        } else {
            return null;
        }

    }

    /**
     * 删除内容中心内容
     *
     */
    public function deleteContent( $arg_array ){

        global $G_SHOP;

        if (!isset($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '缺少参数' );

        if (empty($arg_array['delete_str']))
            return array( 'code' => 400, 'message' => '请选择要删除的内容' );

        $delete_array = explode('_', $arg_array['delete_str']);

        try {

            foreach($delete_array as &$contentID) {

                if (!is_numeric($contentID))
                    return array( 'code' => 400, 'message' => '参数错误' );

                //删除内容中心内容
                $content_center = new entity('content_center');
                $content_center->contentID = $contentID;
                if ($G_SHOP->DBCA->fetch($content_center)) {
                    $G_SHOP->DBCA->delete($content_center);
                }

            }

            return array( 'code' => 200, 'message' => '删除成功' );

        } catch (Exception $e) {

            error_log($e->getMessage());
            return array( 'code' => 500, 'message' => '系统错误' );

        }

    }

    /**
     * 设置内容状态
     *
     * @param array $arg_array
     */
    public function setContentStatus ( $arg_array ){

        if (!isset($arg_array['contentID'],$arg_array['useFlg']) || !is_numeric($_REQUEST['contentID']) || !in_array($arg_array['useFlg'], array('0','1'))) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        $contentID = $arg_array['contentID']; //内容编号
        $useFlg = $arg_array['useFlg'] == 1 ? 0 : 1;   //内容状态

        global $G_PGLOB;

        try {

            //判断内容是否存在
            $content_center = new entity('content_center');
            $content_center->contentID = $contentID;
            if (!$G_PGLOB->DBCA->fetch($content_center)) {
                throw new Exception('内容不存在', 1000);
            }

            $content_center->useFlg = $useFlg;
            $G_PGLOB->DBCA->save($content_center);

            if ($useFlg == 1) {
                return array( 'code' => '200', 'message' => '启用成功' );
            } else {
                return array( 'code' => '200', 'message' => '禁用成功' );
            }

        } catch (Exception $e) {

            if ($e->getCode() == 1000) {
                return array( 'code' => 400, 'message' => $e->getMessage() );
            } else {
                return array( 'code' => 500, 'message' => '系统错误，请联系网站管理员');
            }
        }

    }

    /**
     * 保存内容中心
     * @param 参数数组 $arg_array
     */
    public function saveContentCenter($arg_array){

        global $G_SHOP;

        quanli_include('module/base/common/quanliFile.class.php');

        $quanliFile = new quanliFile();

        //检查参数
        if (!isset($arg_array['content']) || empty($arg_array['content'])) {
            return array('code' => 400, 'message' => '参数错误');
        }

        //add by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---start
        //检查城市
        if ( empty($arg_array['content']['cityID']) && empty($arg_array['content']['cityID']) )
            return array( 'code' => 400, 'message' => '请选择城市' );
        //add by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---start

        //检查外部链接、正文
        if ( empty($arg_array['content']['url_link']) && empty($arg_array['content']['description']) )
            return array( 'code' => 400, 'message' => '请填写外部链接或者正文内容' );

        //排列序号
        if (!isset($arg_array['content']['contentSort']) || empty($arg_array['content']['contentSort'])) {
            return array('code' => 400, 'message' => '请填写排序序号');
        }

        $arg_array['content']['url_link'] = convertVar($arg_array['content']['url_link']);

        //排列序号是否重复
        $check_content_center = new entity('content_center');
        $check_content_center->contentSort = $arg_array['content']['contentSort'];
        if ($G_SHOP->DBCA->fetch($check_content_center)) {
            if (!empty($arg_array['content']['contentID'])) {
                if ($check_content_center->contentID != $arg_array['content']['contentID']) {
                    return array('code' => 400, 'message' => sprintf('排列序号「%s」已经存在',$arg_array['content']['contentSort']));
                }
            } else {
                return array('code' => 400, 'message' => sprintf('排列序号「%s」已经存在',$arg_array['content']['contentSort']));
            }

        }

        //生成新的图片地址
        if($arg_array['content']['content_pic']){
            $new_file_address = $quanliFile->basename_db($arg_array['content']['content_pic']);
        }

        $save_content_center = new entity('content_center');
        if (isset($arg_array['content']['contentID']) && !empty($arg_array['content']['contentID'])) {
            $save_content_center->contentID = $arg_array['content']['contentID'];
            if (!$G_SHOP->DBCA->fetch($save_content_center)) {
                return array('code' => 400, 'message' => '内容中心保存失败');
            }

        } else {
            $save_content_center->creator = $arg_array['creator'];
        }

        $save_content_center->contentTitle 	 = $arg_array['content']['contentTitle'];
        //mod by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---start
        if (!(isset($arg_array['content']['contentID']) && !empty($arg_array['content']['contentID']))) {
            $save_content_center->useFlg  = 1;
        }
        //mod by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---end
        $save_content_center->contentSort 	 = $arg_array['content']['contentSort'];
        $save_content_center->contentSummary = $arg_array['content']['contentSummary'];
        $save_content_center->searchKeys 	 = $arg_array['content']['searchKeys'];
        $save_content_center->isethead 	     = $arg_array['content']['isethead'];
        $save_content_center->isetstern 	 = $arg_array['content']['isetstern'];
        $save_content_center->description = $arg_array['content']['description'];
        $save_content_center->url_link = $arg_array['content']['url_link'];

        if (isset($arg_array['content']['content_pic'])) {
            $save_content_center->content_pic = $arg_array['content']['content_pic'];
        }

        if($arg_array['content']['content_pic']){
            $save_content_center->content_pic = $new_file_address;
        }

        //add by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---start
        $save_content_center->cityID = $arg_array['content']['cityID'];
        $save_content_center->cityName = $arg_array['content']['cityName'];
        //add by allen.qiang 2015-01-13 FUN-BK-0045-PC_WAP首页底下广告位按城市维护 ---end

        $G_SHOP->DBCA->save($save_content_center);

        if (isset($arg_array['content']['contentID']) && !empty($arg_array['content']['contentID'])) {
            return array('code' => 200, 'message' => '内容中心更新成功', 'type' => 2);
        } else {
            return array('code' => 200, 'message' => '内容中心保存成功', 'type' => 1);
        }
    }



 }